//
//  VacationBook.m
//  Vacations
//
//  Created by User-23 on 10/31/14.
//  Copyright (c) 2014 TrainingCamp. All rights reserved.
//

#import "VacationBook.h"
#import "Info.h"


@interface VacationBook ()

@property (nonatomic)  NSMutableArray* allVacations;
@property NSMutableArray* bookedVacation;

@end

const NSInteger kMonasteryIndex = 0;
const NSInteger kVillaIndex = 1;
const NSInteger kHotelIndex = 2;

@implementation VacationBook

+(instancetype)sheredVacationBook
{
    static VacationBook* instanse = nil;
    if (!instanse) {
        instanse = [[super alloc] init];
    }
    return instanse;
}

+(instancetype) alloc
{
    return [VacationBook sheredVacationBook];
}

-(id)copy
{
    return [VacationBook sheredVacationBook];
}

-(NSMutableArray*) allVacations
{
    if (!_allVacations) {
        // create database for info :)
        Info* placeInfo = [[Info alloc] initForInfo];
        [placeInfo addInfo:@"monasteryInfo"];
        [placeInfo addInfo:@"villaInfo"];
        [placeInfo addInfo:@"hotelInfo"];
        
        // create data baze for location
        Info* placeLocation = [[Info alloc] initForLocation];
        [placeLocation addLocationInfo:@"Sofia"];
        [placeLocation addLocationInfo:@"Troqn"];
        [placeLocation addLocationInfo:@"Velingrad"];
        
        Vacation* firstVacation = [[Vacation alloc] initWithName:@"St. Mina" andWithType: Monastery andWithInfo:[placeInfo backInfo:kMonasteryIndex] andWithLocation:[placeLocation backLocationInfo:kMonasteryIndex] andWithOpenDays:@[@"Wednesday",@"Friday",@"Saturday",@"Sunday"] andWithPrice:@0];
        Vacation* secondVacation = [[Vacation alloc] initWithName:@"Beklemeto" andWithType:Villa andWithInfo:[placeInfo backInfo:kVillaIndex] andWithLocation:[placeInfo backLocationInfo:kVillaIndex] andWithOpenDays:@[@"Friday",@"Saturday",@"Sunday",@"Monday",@"Wednesday"] andWithPrice:@90];
        Vacation* thirdVacation = [[Vacation alloc] initWithName:@"Grand Hotel Velingrad"  andWithType: Hotel andWithInfo:[placeInfo backInfo:kHotelIndex] andWithLocation:[placeLocation backLocationInfo:kHotelIndex] andWithOpenDays:@[@"Wednesday",@"Thursday",@"Saturday",@"Sunday"] andWithPrice:@130];
        
        self.allVacations = [[NSMutableArray alloc] initWithObjects:firstVacation, secondVacation, thirdVacation, nil];
    }
    return _allVacations;
}

-(void) addVacation:(Vacation*) vacation
{
    if (vacation) {
        [self.allVacations addObject:vacation];
    }
}

-(void) removeVacation:(Vacation*) vacation
{
    for (NSInteger index = 0 ; index < [self.allVacations count]; index++) {
        if ([vacation isEqual: self.allVacations[index]]) {
            [self.allVacations removeObject:vacation];
        }
    }
}
// get vacation from allVacation array
-(Vacation*) returnVacation:(NSInteger) index
{
    return self.allVacations[index];
}

// add vacation to bookedVacation array
-(void) bookVacation:(Vacation*) vacation
{
    if ((vacation ) && [self.allVacations containsObject:vacation]) {
        [self.bookedVacation addObject:vacation];
        //NSLog(@"rezerveto e ok");
    }
}

// remove vacation from bookedVacation array
-(void) inactiveVacation:(Vacation *) vacation
{
    if (vacation && [self.bookedVacation containsObject:vacation])  {
        [self.bookedVacation removeObject:vacation];
    }
}

// generate random vacation from all
-(Vacation*) generateVacation
{
    NSInteger random = arc4random() %[self.allVacations count];
    NSLog(@"%d",random);
    return self.allVacations[random];
}
// this will increase vacation price when app back from background
-(void) increaseVacationPrice
{
    for (NSInteger index = 0; index < [self.allVacations count]; index++) {
        Vacation* vacTest = self.allVacations[index];
        [vacTest changePrice];
    }
}

-(void) reviewGrow:(Vacation*) vacation
{
    for (NSInteger index = 0; index < [self.allVacations count]; index++) {
        if ([vacation isEqual:self.allVacations[index]]) {
            [vacation increaseReviewCount];
        }
    }
}

@end
